package com.initialize.pub.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.Random;

import static com.initialize.pub.util.MD5.md52;

/**
 * @program: star-blockchain
 * @description: 短信相关的工具
 * @author: Mr.Jane
 * @create: 2019-09-21 23:57
 **/
@Component
public class ToolUtil {

    Logger logger = LoggerFactory.getLogger(ToolUtil.class);

    //生成随机数字(验证码)
    public static String getSJNum() {
        String str = "0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * 生成区块地址
     *
     * @param phone
     * @return
     */
    public String createBelok(String phone) {
        String s = md52(phone);
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("0x").append(s);
        String s1 = md52(s);
        stringBuffer.append(s1, 0, 8);
        return stringBuffer.toString();
    }

    //获取当前时间 生成数据库需要的格式
    public Date createData() {
        Date date = new Date();
        return date;
    }


    //比较时间
    public static long doTimeStampNew(String timestamp) {
        long time = Long.parseLong(timestamp);
        long l = System.currentTimeMillis();
        return (l - time) / 1000;
    }

    //计算多少多少天以后的日期
    public static Date theDateAfterTheCalculation(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();

    }

    //手机号打码
    public static String fuzzyPhoneNumber(String s) {
        return s.substring(0, 3) +
                "****" +
                s.substring(7, 11);
    }

    /**
     * 接收到前端的文件进行类型转换
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static File MultipartFileToFile(MultipartFile multipartFile) throws IOException {
        File toFile;
        if (!multipartFile.equals("") && multipartFile.getSize() > 0) {
            InputStream ins;
            ins = multipartFile.getInputStream();
            toFile = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
            OutputStream os = new FileOutputStream(toFile);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
            return initializationFileName(toFile);
        }
        return null;
    }

    /**
     * 初始化文件名称
     * 防止出现相同的文件 导致对象存储覆盖
     *
     * @param toFile
     * @return
     */
    private static File initializationFileName(File toFile) {
        //获取文件名
        String oldFileName = toFile.getName();
        //把文件名放到stringBuffer
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(oldFileName);
        //拼接4位随机数
        String sjNum = ToolUtil.getSJNum();
        stringBuilder.append(sjNum);
        //拼接时间戳
        Date date = new Date();
        stringBuilder.append(date);
        //获取文件格式
        String fileType = FileUtil.getType(toFile);
        //使用MD5产生文件名。之后加上文件格式拼接出完整的文件名
        String newFileName = MD5.md5(stringBuilder.toString()) + fileType;
        //构造出文件
        File temp = new File(newFileName);
        toFile.renameTo(temp);
        return temp;
    }


    /**
     * 去除markdown的杂乱符号
     *
     * @param content
     * @return
     */
    public static String removeMarkdown(String content) {
        String s = ReUtil.delAll("##### |#### |### |## |# ", content);
        s = ReUtil.delAll("`", s);
        s = ReUtil.delAll("> > |> |>", s);
        s = ReUtil.delAll("'|' ", s);
        s = ReUtil.delAll("!\\[\\]\\(.*\\)", s);
//        s = ReUtil.delAll("\n|\t", s);
        return s;
    }

}